package cc;

import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class DrawPic2 {
	private static final String BACKGROUND_IMG = "D:\\Users\\sw\\Desktop\\ceshi-6666\\poster.png"; // 背景图片
	private static final String result_img = "D:/tmp/images/result.jpg"; // 最终输出图片
	private static final String avatar_img = "https://chengxh.cn1.utools.club/upLoad/aaa.jpg";// QQ获取头像接口

	public static void main(String[] args) {
		drawPoster();
	}

	/**
	 * @Author:pibigstar
	 * @Description: 画海报
	 */
	public static void drawPoster() {
		try {
			long startTime = System.currentTimeMillis();
			// 1. 创建画布
			BufferedImage backgroundImg = ImageIO.read(getInputStream(BACKGROUND_IMG));
			BufferedImage canvas = new BufferedImage(backgroundImg.getWidth(), backgroundImg.getHeight(),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = (Graphics2D) canvas.getGraphics();
			// 设置抗锯齿
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
			
			

			// 2. 将头像设置为圆角
			BufferedImage avatar = ImageIO.read(new URL(avatar_img));
			BufferedImage newAvatar = new BufferedImage(avatar.getWidth(), avatar.getHeight(),
					BufferedImage.TYPE_INT_ARGB);
			Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, avatar.getWidth(), avatar.getHeight());
			Graphics2D g2 = newAvatar.createGraphics();
			newAvatar = g2.getDeviceConfiguration().createCompatibleImage(avatar.getWidth(), avatar.getHeight(),
					Transparency.TRANSLUCENT);
			g2 = newAvatar.createGraphics();
			g2.setComposite(AlphaComposite.Clear);
			g2.fill(new Rectangle(newAvatar.getWidth(), newAvatar.getHeight()));
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
			g2.setClip(shape);
			// 使用 setRenderingHint 设置抗锯齿
			g2 = newAvatar.createGraphics();
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g2.fillRoundRect(0, 0, avatar.getWidth(), avatar.getHeight(), 150, 150);
			g2.setComposite(AlphaComposite.SrcIn);
			g2.drawImage(avatar, 0, 0, avatar.getWidth(), avatar.getHeight(), null);
			g2.dispose();

			// 3. 将背景图和头像结合
			// 画背景
			g.drawImage(backgroundImg.getScaledInstance(backgroundImg.getWidth(), backgroundImg.getHeight(),
					Image.SCALE_DEFAULT), 0, 0, null);
			// 背景上画头像
			g.drawImage(newAvatar.getScaledInstance(150, 150, Image.SCALE_DEFAULT), 80, 260, null);

			// 画竖着的文字
			g.dispose();
			File resultImg = new File(result_img);
			ImageIO.write(canvas, "png", resultImg);

			System.out.println("生成成功！");
			System.out.println("耗时: " + (System.currentTimeMillis() - startTime) / 1000.0 + "s");
			System.out.println("生成文件路径: " + resultImg.getAbsolutePath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 获取resources下的文件输入流
	 * @throws FileNotFoundException 
	 */
	private static InputStream getInputStream(String fileName) throws FileNotFoundException {
		File f= new File(fileName);
		InputStream input=new  FileInputStream(f);
		return input;
	}

}
